package service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Treceipt;
import model.Tuser;
import model.Tvip;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.ReceiptDaoI;
import dao.UserDaoI;
import dao.VipDaoI;
import pageModel.DataGrid;
import pageModel.Receipt;
import pageModel.SaleHighchart;
import pageModel.SaleStatistic;
import service.ReceiptServiceI;
@Service("receiptService")
public class ReceiptServiceImpl implements ReceiptServiceI {
private ReceiptDaoI receiptDao;
private VipDaoI vipDao;
private UserDaoI userDao;
	public ReceiptDaoI getReceiptDao() {
	return receiptDao;
}
@Autowired
public void setReceiptDao(ReceiptDaoI receiptDao) {
	this.receiptDao = receiptDao;
}

	public VipDaoI getVipDao() {
	return vipDao;
}
	@Autowired
public void setVipDao(VipDaoI vipDao) {
	this.vipDao = vipDao;
}
public UserDaoI getUserDao() {
	return userDao;
}
   @Autowired
public void setUserDao(UserDaoI userDao) {
	this.userDao = userDao;
}

	@Override
	public Receipt saveOrUpdate(Receipt receipt) {
		if(receipt.getTid()!=null&&!receipt.getTid().trim().equals("")){
			String hql = "from Treceipt t where t.tid=:tid";
			Map<String,Object>params=new HashMap<String,Object>();
			params.put("tid", receipt.getTid());
			Treceipt ot = receiptDao.get(hql, params);
			if(ot!=null){
				ot.setState("支付完成");
				receiptDao.save(ot);
				BeanUtils.copyProperties(ot, receipt);
			}else{
				   Treceipt t=new Treceipt();
				   BeanUtils.copyProperties(receipt,t);
				   Timestamp now = new Timestamp(System.currentTimeMillis()); 
				   t.setDate(now);
				   t.setState("未支付");
				   if(receipt.getVip()!=null&&!receipt.getVip().trim().equals("")){
					  String hql1="from Tvip t where t.name=:name";
					  Map<String,Object> params1=new HashMap<String,Object>();
					  params1.put("name", receipt.getVip());
					  Tvip v=vipDao.get(hql1,params1);			  
					  t.setTvip(v);
				   }
				   if(receipt.getCashier()!=null&&!receipt.getCashier().trim().equals("")){
					   String hql2="from Tuser t where t.logname=:logname";
					   Map<String,Object> params2=new HashMap<String,Object>();
					   params2.put("logname", receipt.getCashier());
					   Tuser u=userDao.get(hql2,params2);
					   t.setTuser(u);
				   }
				   receiptDao.save(t);
				   BeanUtils.copyProperties(t,receipt);
			}
		}
		   return receipt;
	}
	@Override
	public DataGrid datagrid(Receipt receipt) {
		DataGrid dg=new DataGrid();
		String hql="from Treceipt t where 1=1";
		Map<String,Object> params=new HashMap<String,Object>();
		hql = addWhere(receipt, hql, params);
		String totalHql="select count(*) "+hql;
		if(receipt.getSort()!=null){
		hql+=" order by "+receipt.getSort()+" "+receipt.getOrder();
		}
		List<Treceipt> tr=receiptDao.find(hql,params,receipt.getPage(),receipt.getRows());
		List<Receipt> l=new ArrayList<Receipt>();
		if(tr!=null&&tr.size()>0){
			for(Treceipt t :tr){
				Receipt r=new Receipt();
				BeanUtils.copyProperties(t, r);
				if(t.getTuser()!=null){
					r.setCashier(t.getTuser().getLogname());
				}
				if(t.getTvip()!=null){
					r.setVip(t.getTvip().getName());
					r.setCardid(t.getTvip().getCardid());
				}
				l.add(r);
			}
		}
		dg.setTotal(receiptDao.count(totalHql,params));
		dg.setRows(l);
		return dg;
	}
	private String addWhere(Receipt receipt, String hql, Map<String, Object> params) {
		if(receipt.getCashier()!=null&&!receipt.getCashier().trim().equals("")){
		hql+=" and t.tuser.logname like :logname";
		params.put("logname", "%%"+receipt.getCashier()+"%%");
		}
		if(receipt.getCardid()!=null&&!receipt.getCardid().trim().equals("")){
			hql+=" and t.tvip.cardid like :cardid";
			params.put("cardid", "%%"+receipt.getCardid()+"%%");
			}
		if(receipt.getVip()!=null&&!receipt.getVip().trim().equals("")){
			hql+=" and t.tvip.name like :name";
			params.put("name", "%%"+receipt.getVip()+"%%");
			}
		if (receipt.getCreatedatetimeStart() != null) {
			hql += " and t.date >= :createdatetimeStart";
			params.put("createdatetimeStart", receipt.getCreatedatetimeStart());
		}
		if (receipt.getCreatedatetimeEnd() != null) {
			hql += " and t.date <= :createdatetimeEnd";
			params.put("createdatetimeEnd", receipt.getCreatedatetimeEnd());
		}
		return hql;
	}
	@Override
	public DataGrid show(String cashier,Receipt receipt) {
		DataGrid dg=new DataGrid();
		String hql="from Treceipt t where t.state=:state and t.tuser.logname=:logname";
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("state", "未支付");
		params.put("logname", receipt.getCashier());
		String totalHql="select count(*) "+hql;
		if(receipt.getSort()!=null){
		hql+=" order by "+receipt.getSort()+" "+receipt.getOrder();
		}
		List<Treceipt> tr=receiptDao.find(hql,params,receipt.getPage(),receipt.getRows());
		List<Receipt> l=new ArrayList<Receipt>();
		if(tr!=null&&tr.size()>0){
			for(Treceipt t :tr){
				Receipt r=new Receipt();
				BeanUtils.copyProperties(t, r);
				if(t.getTuser()!=null){
					r.setCashier(t.getTuser().getLogname());
				}
				if(t.getTvip()!=null){
					r.setVip(t.getTvip().getName());
					r.setCardid(t.getTvip().getCardid());
					r.setIntegration(t.getTvip().getIntegration());
					r.setDiscount(t.getTvip().getDiscount());
				}
				l.add(r);
			}
		}
		dg.setTotal(receiptDao.count(totalHql,params));
		dg.setRows(l);
		return dg;
	}
	@Override
	public List<SaleStatistic> jasper(Receipt receipt) {
		String hql="select new Map(year(t.date) as year,month(t.date) as month, sum(t.totalprice) as total) from Treceipt t where 1=1";
		Map<String,Object> params=new HashMap<String,Object>();
		if (receipt.getCreatedatetimeStart() != null) {
			hql += " and t.date >= :createdatetimeStart";
			params.put("createdatetimeStart", receipt.getCreatedatetimeStart());
		}
		if (receipt.getCreatedatetimeEnd() != null) {
			hql += " and t.date <= :createdatetimeEnd";
			params.put("createdatetimeEnd", receipt.getCreatedatetimeEnd());
		}
		if (receipt.getCreatedatetimeStart() == null&&receipt.getCreatedatetimeEnd() == null) {
			Calendar now = Calendar.getInstance(); 
			int year =  now.get(Calendar.YEAR);
			hql += " and year(t.date)= :yeartime";
			params.put("yeartime", year);
		}
		String totalHql=hql + " group by month(t.date)";
		List<Map<String, Object>> l = receiptDao.findByMap(totalHql,params);
		List<SaleStatistic> ls = new ArrayList<>();
		for(Map<String, Object> map : l){
			SaleStatistic s = new SaleStatistic();
			s.setYear((Integer)map.get("year"));
			s.setMonth((Integer)map.get("month"));
			s.setTotal((Double)map.get("total"));
			ls.add(s);
		}
		return ls;
	}
	@Override
	public List<SaleHighchart> highChart(Receipt receipt) {
		String hql="select new Map(year(t.date) as year,month(t.date) as month, sum(t.totalprice) as total) from Treceipt t where 1=1";
		Map<String,Object> params=new HashMap<String,Object>();
		if (receipt.getCreatedatetimeStart() != null) {
			hql += " and t.date >= :createdatetimeStart";
			params.put("createdatetimeStart", receipt.getCreatedatetimeStart());
		}
		if (receipt.getCreatedatetimeEnd() != null) {
			hql += " and t.date <= :createdatetimeEnd";
			params.put("createdatetimeEnd", receipt.getCreatedatetimeEnd());
		}
		if (receipt.getCreatedatetimeStart() == null&&receipt.getCreatedatetimeEnd() == null) {
			Calendar now = Calendar.getInstance(); 
			int year =  now.get(Calendar.YEAR);
			hql += " and year(t.date)= :yeartime";
			params.put("yeartime", year);
		}
		String totalHql=hql + " group by month(t.date)";
		List<Map<String, Object>> l = receiptDao.findByMap(totalHql,params);
		List<SaleHighchart> ls = new ArrayList<>();
		SaleHighchart s = new SaleHighchart();
		List<Object> listdata=new ArrayList<>();
		for(Map<String, Object> map : l){
			List<Object> data=new ArrayList<>();
			s.setName("销售额");
			data.add((Integer)map.get("month"));
			data.add((Double)map.get("total"));
			listdata.add(data);
		}
		s.setData(listdata);
	    ls.add(s);
		return ls;
	}
}
